package com.smartalarmclock.UI;

import java.util.ArrayList;
import java.util.Set;

import com.smartalarmclock.Logic.AcManager;
import com.smartalarmclock.Model.IContact;
import com.smartalarmclock.Model.IGroup;
import com.smartalarmclock.Model.ISingleContact;
import com.smartalarmclock.Model.Exceptions.PhoneContactDoesNotExistException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.ListView;

public final class SelectContactsDialogFragment extends DialogFragment {
	private Activity m_Activity;
	private Set<IContact> m_SelectedContacts = null;
	
	private boolean m_SelectMultipleContacts;
	private boolean m_AddGroups;
	
	private static final String c_ArgumentSelectMultipleContacts = "SELECT_MULTIPLE";
	private static final String c_ArgumentUseGroups = "USE_GROUPS";
	
	/**
	 * Creates and returns a new instance while setting the necessary arguments.
	 * @param selectMultipleContacts whether the dialog should allow multiple selection of contacts
	 * @param addGroups whether the dialog should add the groups to the list of contacts for selection
	 * @return the new instance of SelectContactsDialogFragment
	 */
	public static SelectContactsDialogFragment NewInstance(boolean selectMultipleContacts, boolean addGroups) {
		SelectContactsDialogFragment fragment = new SelectContactsDialogFragment();
		
		Bundle arguments = new Bundle();
		arguments.putBoolean(c_ArgumentSelectMultipleContacts, selectMultipleContacts);
		arguments.putBoolean(c_ArgumentUseGroups, addGroups);
		fragment.setArguments(arguments);
		
		return fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Getting the arguments of the dialog fragment
		Bundle arguments = getArguments();
		m_SelectMultipleContacts = arguments.getBoolean(c_ArgumentSelectMultipleContacts);
		m_AddGroups = arguments.getBoolean(c_ArgumentUseGroups);
	}
	
	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Getting contacts to display in the contacts list
		final ArrayList<IContact> contacts = new ArrayList<IContact>();
		for (ISingleContact contact : AcManager.GetInstance().GetAllSingleContacts(m_Activity)) {
			contacts.add(contact);
		}
		if (m_AddGroups) {
			for (IGroup contact : AcManager.GetInstance().GetAllGroups(m_Activity)) {
				contacts.add(contact);
			}
		}
		
		// Creating the array of the contacts and the matching array of the selected contacts for the dialog
		int listLength = contacts.size();
		CharSequence[] contactItems = new CharSequence[listLength];
		boolean[] checkedContacts = new boolean[listLength];
		
		// Adding all contacts
		for (int position = 0; position < listLength; position++) {
			try {
				contactItems[position] = contacts.get(position).GetName(m_Activity);
				
				// Setting the contact as selected if it is already set
				checkedContacts[position] = m_SelectedContacts.contains(contacts.get(position)) ? true : false;
				
			} catch (PhoneContactDoesNotExistException e) {
				// The single contact is not in the phone contacts list. Skipping it.
			}
		}
		
		// Getting the title of the dialog
		int titleId = m_SelectMultipleContacts ? R.string.select_contacts : R.string.select_contact;
		
		// Building the dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(m_Activity);
		builder.setTitle(titleId);
		
		builder.setPositiveButton(R.string.select_button_text, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					((IContactSelector)m_Activity).OnSelected(m_SelectedContacts);
				}
			});
		
		builder.setNegativeButton(R.string.cancel_button_text, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					((IContactSelector)m_Activity).OnCanceled();
					dialog.dismiss();
				}
			});
		
		// Setting the items and the choice settings (single or multiple)
		if (!m_SelectMultipleContacts) {
			builder.setSingleChoiceItems(contactItems, -1,	new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// Setting single selected contact
					m_SelectedContacts.clear();
					m_SelectedContacts.add(contacts.get(which));
				}
			});
		}
		else {
			builder.setMultiChoiceItems(contactItems, checkedContacts, new DialogInterface.OnMultiChoiceClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					// Adding selected contact to the list
					IContact selectedContact = contacts.get(which);
					if (isChecked) {
						m_SelectedContacts.add(selectedContact);
					}
					else {
						m_SelectedContacts.remove(selectedContact);
					}
				}
			});
		}
		
		return builder.create();
    }
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		m_Activity = activity;
		m_SelectedContacts = ((IContactSelector)m_Activity).GetCurrentlySelectedContacts();
	}
}
